﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrickLevelEditor.Models;
using System.IO;
using GMLogging;
using System.Windows.Media.Imaging;
using GMUtilitiesDotNet.Helpers;

namespace ThouShaltNotBrickLevelEditor.DataAccess
{
    public static class BrickStateImagesLoader
    {
        #region Methods
        public static BrickStateImages Load(string pBrickStateImagesFolderPath)
        {
            DirectoryInfo dir = new DirectoryInfo(pBrickStateImagesFolderPath);
            if (!dir.Exists)
            {
                Exception e = new DirectoryNotFoundException();
                Logger.Error(e.Message);
                throw e;
            }

            Dictionary<string, BitmapImage> brickStateImageDict = new Dictionary<string, BitmapImage>();

            FileInfo[] files = dir.GetFiles("*.png");
            foreach (FileInfo file in files)
            {
                string brickStateName = Path.GetFileNameWithoutExtension(file.Name);
                Uri uri = new Uri(file.FullName);
                BitmapImage brickStateImage = IOHelpers.GetBitmapImageWithChecks(uri);
                brickStateImageDict[brickStateName] = (BitmapImage)brickStateImage.GetAsFrozen();
            }

            BrickStateImages brickStateImages = new BrickStateImages(brickStateImageDict);
            return brickStateImages;
        }
        #endregion
    }
}